Sample 12 ODE




SYSTEM(RUN=1) DLG(TItle='Ordinary Differential Equations', E='#Describe', B='OK')

#Describe . A 4th order Runge-Kutta-algorithm is used . for the numerical integration of . Ordinary Differential Equations. Call: . ODE(y0, $xmin, $xmax, steps, 'f0(x, y, dy, $result)') with: . yo the initial value (at x=$xmin) . $xmin intial value of the independent variable . $xmax final value of the independent variable . steps number of steps for the integration . f0 the name of the integration callback FUNCTION . with: x name of the independent variable . y name of the dependent variable . dy derivative dy(x,y)/dx . $result result matrix for graphics. ODE will write: . column 1: x . column 2: integration result(x) . column 3: these examples: theoretical value
SET($nr=1, $type="y'=5*t^4", t=0, x=0, steps=20, $xmin=0, $xmax=5, resultLines=0,dtFMT=" ") radio = ("y'=5*t^4", "y'+y=SIN(t)", "y'+x*y=x*y^2", "y'=y*x^2/(1+x^3)") DO $nr = INDEX(radio, $type, 'Array') SET(y0=(1,1), y=(1,1), dy=(1,1), $result=(1,3)) IF($nr == 1) y0(1) = $xmin^5 ELSEIF($nr == 2) y0(1) = (EXP(0-$xmin) + SIN($xmin) - COS($xmin)) / 2 ELSEIF($nr == 3) y0(1) = 1/(EXP($xmin^2/2) + 1) ELSEIF($nr == 4) y0(1) = (4*(1+$xmin^3)) ^ (1/3) ENDIF TIME(SECondssince = t0) ODE(y0, $xmin, $xmax, steps, 'f0(t, y, dy, $result)') dTime = TIME(SECondssince=t0) dtFmt = FMT(dTime, 'F3') && 'seconds to execute' resultLines = INT(LEN($result)) Graphics(dtFmt) DLG(BG=90, TI=radio($nr), RAD=radio,Val=$type, B='OK', NE=y0(1),SYM, NE=steps,SYM, NE=$xmin,SYM, NE=$xmax,SYM, LBL=dtFMT,SYM, LBL=resultLines,SYM, B='Cancel', B='Cancel') ARRAY(Name=$result, CLeaR=1) ENDDO ENDDO END FUNCTION f0(t, y, dy, $result) IF($nr == 1) dy(1) = 5*t^4 ELSEIF($nr == 2) dy(1) = SIN(t) - y(1) ELSEIF($nr == 3) dy(1) = t*y(1)*(y(1) - 1) ELSEIF($nr == 4) dy(1) = y(1)*t^2/(1+t^3) ENDIF END FUNCTION Graphics(dt) xTitle = $type & ', blue=exact, green=numeric integration' DO i = 1, LEN($result) t = $result(i,1) IF($nr == 1) $result(i,3) = t^5 ELSEIF($nr == 2) ! y=cEXP(-x)+(SIN(x)-COS(x))/2 y=0 @ x=0 => c=1/2 $result(i,3) = (EXP(-t) + SIN(t) - COS(t)) / 2 ELSEIF($nr == 3) ! y=2 @ t=1 $result(i,3) = 1/(EXP(t^2/2) + 1) ELSEIF($nr == 4) ! y^3 = 4(1+x^3) $result(i,3) = (4*(1+t^3)) ^ (1/3) ENDIF ENDDO SEt(yMin=1E100, yMax=-1E100) DO i = 1, LEN($result) yMin = MIN(ymin, $result(i,2), $result(i,3)) IF((yMax > 1E100) + (ymin < -1E100)) MSG(T='Will run beyond limits :||increase steps,|and/or|decrease tmax||for this problem', B='OK') EXIT ENDIF yMax = MAX(yMax, $result(i,2), $result(i,3)) ENDDO DLG(L=0.6,H=1,T=0,W=0.4, TI=dt, AX=3, TI=xTitle, MIN=$xmin, MAX=$xmax, Y=1, MIN=yMin, Max=yMax) LINE(AX=3, Column=1, Zeros=1, XVEC=$result, Col=2, YVEC=$result, Width=19, Draw=90) LINE(AX=3, Column=1, Zeros=1, XVEC=$result, Col=3, YVEC=$result, Width=1, Draw=9) END